package codeRandomThoughts.Test122买卖股票的最佳时机II;

public class Solution {
    public int maxProfit(int[] prices) {
        int buyPrice = prices[0];
        int sellPrice = prices[0];
        int sum = 0;

        for (int i = 0; i < prices.length - 1; i++) {
            //如果知道第二天要下跌,就在今天出售股票
            if (prices[i + 1] < sellPrice) {
                sum += (sellPrice - buyPrice);
                buyPrice = prices[i + 1];
                sellPrice = prices[i + 1];
            } else {
                //如果明天上涨,sellPrice直接变成明天的价格
                sellPrice = prices[i + 1];
            }
        }

        //最后一天也要把手里的股票卖了
        sum += (sellPrice - buyPrice);
        return sum;
    }
}
